import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {GetResponseNormal} from "./GetResponseNormal";
import {GetResponseNormalList} from "./GetResponseNormalList";
import {GetResponseRecord} from "./GetResponseRecord";
import {GetResponseRecordList} from "./GetResponseRecordList";
import {GetResponseNext} from "./GetResponseNext";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";

enum Choice {
    _ERR_NONE_SELECTED = -1,
    GET_RESPONSE_NORMAL = 1,
    GET_RESPONSE_NORMAL_LIST = 2,
    GET_RESPONSE_RECORD = 3,
    GET_RESPONSE_RECORD_LIST = 4,
    GET_RESPONSE_NEXT = 5,
}

export class Get_Response implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    getResponseNormal : GetResponseNormal | null = null;
    getResponseNormalList : GetResponseNormalList | null = null;
    getResponseRecord : GetResponseRecord | null = null;
    getResponseRecordList : GetResponseRecordList | null = null;
    getResponseNext : GetResponseNext | null = null;


    constructor() {
    }

    set_dataCode(dataCode: Buffer) {
        this.dataCode = dataCode;
    }

    setGetResponseNormal(getResponseNormal : GetResponseNormal) {
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_NORMAL;
        this.getResponseNormal = getResponseNormal;
    }

    setGetResponseNormalList(getResponseNormalList : GetResponseNormalList){
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_NORMAL_LIST;
        this.getResponseNormalList = getResponseNormalList;
    }

    setGetResponseRecord(getResponseRecord : GetResponseRecord){
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_RECORD;
        this.getResponseRecord = getResponseRecord;
    }

    setGetResponseRecordList(getResponseRecordList : GetResponseRecordList){
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_RECORD_LIST;
        this.getResponseRecordList = getResponseRecordList;
    }

    setGetResponseNext(getResponseNext : GetResponseNext){
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_NEXT;
        this.getResponseNext = getResponseNext;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            this.getResponseNormal = new GetResponseNormal();
            codeLength += this.getResponseNormal.decode(input);
            return codeLength;
        }else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            this.getResponseNormalList = new GetResponseNormalList();
            codeLength += this.getResponseNormalList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            this.getResponseRecord = new GetResponseRecord();
            codeLength += this.getResponseRecord.decode(input);
            return codeLength;
        }else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            this.getResponseRecordList = new GetResponseRecordList();
            codeLength += this.getResponseRecordList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.GET_RESPONSE_NEXT) {
            this.getResponseNext = new GetResponseNext();
            codeLength += this.getResponseNext.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.GET_RESPONSE_NEXT) {
            if(this.getResponseNext != null){
                codeLength += this.getResponseNext.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(5);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getResponseNext is null");
            }
        }else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            if(this.getResponseRecordList != null){
                codeLength += this.getResponseRecordList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(4);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getResponseRecordList is null");
            }
        }else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            if(this.getResponseRecord != null){
                codeLength += this.getResponseRecord.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(3);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getResponseRecord is null");
            }
        }else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            if(this.getResponseNormalList != null){
                codeLength += this.getResponseNormalList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(2);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getResponseNormalList is null");
            }
        }else if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            if(this.getResponseNormal != null){
                codeLength += this.getResponseNormal.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getResponseNormal is null");
            }
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.getResponseNormal = null;
        this.getResponseNormalList = null;
        this.getResponseRecord = null
        this.getResponseRecordList = null;
        this.getResponseNext = null;
    }

    toString(): string {
        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            if (this.getResponseNormal != null) {
                return "choice: {getResponseNormal: " + this.getResponseNormal + "}";
            } else {
                return "choice is getResponseNormal but getResponseNormal is null";
            }
        }else if(this.choice == Choice.GET_RESPONSE_NORMAL_LIST){
            if(this.getResponseNormalList != null){
                return "choice: {getResponseNormalList: " + this.getResponseNormalList + "}";
            }else{
                return "choice is getResponseNormalList but getResponseNormalList is null";
            }
        }else if(this.choice == Choice.GET_RESPONSE_RECORD){
            if(this.getResponseRecord != null){
                return "choice: {getResponseRecord: " + this.getResponseRecord + "}";
            }else{
                return "choice is getResponseRecord but getResponseRecord is null";
            }
        }else if(this.choice == Choice.GET_RESPONSE_RECORD_LIST){
            if(this.getResponseRecordList != null){
                return "choice: {getResponseRecordList: " + this.getResponseRecordList + "}";
            }else{
                return "choice is getResponseRecordList but getResponseRecordList is null";
            }
        }else if(this.choice == Choice.GET_RESPONSE_NEXT){
            if(this.getResponseNext != null){
                return "choice: {getResponseNext: " + this.getResponseNext + "}";
            }else{
                return "choice is getResponseNext but getResponseNext is null";
            }
        } else {
            return "unknown";
        }
    }

}
